package com.gzdh.dishrecognition.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.ui.base.BaseMVCActivity;
import com.gzdh.dishrecognition.ui.fragment.FragmentAdapter;
import com.gzdh.dishrecognition.ui.fragment.report.DeviceOrderReportFragment;
import com.gzdh.dishrecognition.ui.fragment.report.DinnerTableReportFragment;
import com.gzdh.dishrecognition.ui.fragment.report.LcReportFragment;
import com.gzdh.dishrecognition.ui.holde.ButtonLayoutHolder;
import com.gzdh.dishrecognition.ui.holde.ButtonView;
import com.gzdh.dishrecognition.ui.listnenr.FragmentKeyEventListener;
import com.gzdh.dishrecognition.ui.view.MViewPager;

import static androidx.fragment.app.FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT;

import java.util.ArrayList;
import java.util.List;

public class ReportActivity extends BaseMVCActivity {

    private Window window = null;

    public MViewPager fragmentViewPager;

    private ListAdapter listAdapter;

    private Context mContext;

    private List<Fragment> fragmentlist;

    private TextView titleTv;

    private ImageView backIv;

    FragmentKeyEventListener fragmentKeyEventListener;

    private Integer currentItem = 0;

    private DeviceOrderReportFragment deviceOrderReportFragment;

    public void setFragmentKeyEventListener(FragmentKeyEventListener fragmentKeyEventListener) {
        this.fragmentKeyEventListener = fragmentKeyEventListener;
    }

    public static void start(Context context) {
        context.startActivity(new Intent(context, ReportActivity.class));
    }

    public static void startAndSelectItem(Context context, String item) {
        Intent intent = new Intent(context, ReportActivity.class);
        intent.putExtra("currentItem", item);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sys_menu;
    }

    @Override
    protected void initView() {
        mContext = this;
        String item = getIntent().getStringExtra("currentItem");
        if(item != null) {
            currentItem = Integer.parseInt(item);
            findViewById(R.id.leftLinear).setVisibility(View.GONE);
        }
        titleTv = findViewById(R.id.titleTv);
        titleTv.setText("查看报表");
        fragmentViewPager = (MViewPager) findViewById(R.id.fragmentViewPager);
        ListView leftListView = findViewById(R.id.leftListView);
        ArrayList<ButtonView> buttonListView = new ArrayList<>();
        backIv = findViewById(R.id.backBut);
        backIv.setOnClickListener(view -> finish());
        buttonListView.add(new ButtonView("查看领餐", R.mipmap.sysinfo_icon));
        buttonListView.add(new ButtonView("设备订单", R.mipmap.sysinfo_icon));
        buttonListView.add(new ButtonView("未翻台订单", R.mipmap.sysinfo_icon));
        buttonListView.add(new ButtonView("已翻台订单", R.mipmap.sysinfo_icon));
        listAdapter = new ListAdapter(buttonListView);
        leftListView.setAdapter(listAdapter);
        leftListView.setDividerHeight(0);
        leftListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                listAdapter.setSelectedPosition(position);
                listAdapter.notifyDataSetInvalidated();
                switch (position) {
                    case 0:
                        fragmentViewPager.setCurrentItem(0);
                        break;
                    case 1:
                        fragmentViewPager.setCurrentItem(1);
                        break;
                    case 2:
                        fragmentViewPager.setCurrentItem(2);
                        break;
                    case 3:
                        fragmentViewPager.setCurrentItem(3);
                        break;
                    case 4:
                        fragmentViewPager.setCurrentItem(4);
                        break;
                }
            }
        });
    }

    @Override
    protected void initData() {
        initDatasViewPager();
        fragmentViewPager.setCurrentItem(currentItem);
    }

    @Override
    protected void dissmissPresnetation() {

    }


    public class ListAdapter extends BaseAdapter {

        ArrayList<ButtonView> arrayList = null;
        LayoutInflater inflater;
        View view;
        ButtonLayoutHolder buttonLayoutHolder;
        RelativeLayout buttonLayout = null;
        TextView buttonText = null;
        ImageView imagesView = null;


        private int selectedPosition = currentItem;// 选中的位置

        public ListAdapter(ArrayList<ButtonView> buttonListView) {
            // TODO Auto-generated constructor stub
            arrayList = buttonListView;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return arrayList.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return arrayList.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public void setSelectedPosition(int position) {
            selectedPosition = position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
//            inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            inflater = LayoutInflater.from(mContext);
            view = inflater.inflate(R.layout.item_vh_layout, null, false);
            buttonLayoutHolder = (ButtonLayoutHolder) view.getTag();
            if (buttonLayoutHolder == null) {
                buttonLayoutHolder = new ButtonLayoutHolder();
                buttonLayoutHolder.buttonLayout = (RelativeLayout) view.findViewById(R.id.LinearLayoutButton);
                buttonLayoutHolder.textView = (TextView) view.findViewById(R.id.TextViewButton);
                buttonLayoutHolder.imagesView = (ImageView) view.findViewById(R.id.images);
                view.setTag(buttonLayoutHolder);
            }
            buttonLayout = buttonLayoutHolder.buttonLayout;
            buttonText = buttonLayoutHolder.textView;
            imagesView = buttonLayoutHolder.imagesView;
            if (selectedPosition == position) {
                buttonText.setSelected(true);
                buttonText.setPressed(true);
                buttonLayout.setBackgroundColor(Color.parseColor("#e4e8e9"));
                buttonText.setTextColor(Color.BLUE);
            } else {
                buttonText.setSelected(false);
                buttonText.setPressed(false);
                buttonLayout.setBackgroundColor(Color.parseColor("#002f4471"));
                buttonText.setTextColor(Color.BLACK);
            }
            imagesView.setImageResource(arrayList.get(position).imageId);
            buttonText.setText(arrayList.get(position).textViewId);
            return view;
        }
    }


    public void initDatasViewPager() {
        fragmentlist = new ArrayList<>();
        fragmentlist.add(new LcReportFragment(this));
        deviceOrderReportFragment = new DeviceOrderReportFragment(this);
        fragmentlist.add(deviceOrderReportFragment);
        fragmentlist.add(new DinnerTableReportFragment(this, 0));
        fragmentlist.add(new DinnerTableReportFragment(this, 1));
        fragmentViewPager.setScanScroll(false);
        fragmentViewPager.setAdapter(new FragmentAdapter(getSupportFragmentManager(), fragmentlist, this, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT));
    }


    @Override
    public void initPresentation() {
        super.initPresentation();

    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if(event.getKeyCode() == KeyEvent.KEYCODE_F1 && event.getAction() == KeyEvent.ACTION_DOWN) {
            deviceOrderReportFragment.reload();
            return true;
        }
        if(event.getKeyCode() == KeyEvent.KEYCODE_F3 && event.getAction() == KeyEvent.ACTION_DOWN) {
            backIv.performClick();
            return true;
        }
        if(event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_ADD && event.getAction() == KeyEvent.ACTION_DOWN) {
            deviceOrderReportFragment.scrollView(0, -100);
            return true;
        }
        if(event.getKeyCode() == KeyEvent.KEYCODE_NUMPAD_SUBTRACT && event.getAction() == KeyEvent.ACTION_DOWN) {
            deviceOrderReportFragment.scrollView(0, 100);
            return true;
        }
        if (event.getKeyCode() != KeyEvent.KEYCODE_BACK) {
            if (fragmentKeyEventListener != null) {
                fragmentKeyEventListener.onFragmentKeyEvent(event);
            }
            return true;
        }
        return super.dispatchKeyEvent(event);
    }

}
